﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Xiaowei.Models;
using Xiaowei.Services;
using Xiaowei.ViewModel;

namespace Xiaowei
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MiniModeMainWindow : Window
    {
        public MiniModeMainWindow()
        {
            InitializeComponent();
            DataContext = MainWindowViewModel;
            MainWindowViewModel.History.CollectionChanged += History_CollectionChanged;
            SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
            ResetSize();
        }

        private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            ResetSize();
        }

        private void ResetSize()
        {
            Left = SystemParameters.WorkArea.X;
            Top = SystemParameters.WorkArea.Y;
            Width = SystemParameters.WorkArea.Width;
            Height = SystemParameters.WorkArea.Height;
        }


        private async void History_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if(e.Action== System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                await Task.Delay(100);
                xiaoweiPanel.ShowLastItem();
            }

        }

        private bool isListening = false;
        public MainWindowViewModel MainWindowViewModel { get; } = new MainWindowViewModel();

        private void mainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ResetPanelPosition();
        }

        private void mainWindow_MouseEnter(object sender, MouseEventArgs e)
        {
           
        }

        private void mainWindow_MouseLeave(object sender, MouseEventArgs e)
        {
        }

        private void mainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            MainWindowViewModel.Dispose();
        }

        private void mainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ResetPanelPosition();
        }
        private void ResetPanelPosition()
        {
            var left = innerPanel.ActualWidth - xiaoweiPanel.ActualWidth - 10;
            var top = innerPanel.ActualHeight - xiaoweiPanel.ActualHeight - 10;
            Canvas.SetLeft(xiaoweiPanel,left);
            Canvas.SetTop(xiaoweiPanel, top);
        }

        private void xiaoweiPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ResetPanelPosition();
        }
    }
}
